/**
 * Copyright (c) 2011 ~ 2013 Deepin, Inc.
 *               2011 ~ 2013 jouyouyun
 *
 * Author:      jouyouyun <jouyouwen717@gmail.com>
 * Maintainer:  jouyouyun <jouyouwen717@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 **/

package main

import (
        "dlib/glib-2.0"
        liblogger "dlib/logger"
        "os"
)

var logger = liblogger.NewLogger("dde-api/utils")

func writeKeyToKeyFile(filename, group, key string, value interface{}) bool {
        if len(filename) <= 0 {
                return false
        }

        keyFile := glib.NewKeyFile()
        defer keyFile.Free()
        _, err := keyFile.LoadFromFile(filename,
                glib.KeyFileFlagsKeepComments)
        if err != nil {
                logger.Warning("LoadFile '%s' failed: %v\n", filename, err)
                return false
        }

        switch value.(type) {
        case bool:
                keyFile.SetBoolean(group, key, value.(bool))
        case []bool:
                keyFile.SetBooleanList(group, key, value.([]bool))
        case int, int32, uint32:
                keyFile.SetInteger(group, key, value.(int))
        case []int, []int32, []uint32, []int64, []uint64:
                keyFile.SetIntegerList(group, key, value.([]int))
        case int64:
                keyFile.SetInt64(group, key, value.(int64))
        case uint64:
                keyFile.SetUint64(group, key, value.(uint64))
        case float32, float64:
                keyFile.SetDouble(group, key, value.(float64))
        case []float32, []float64:
                keyFile.SetDoubleList(group, key, value.([]float64))
        case string:
                keyFile.SetString(group, key, value.(string))
        case []string:
                keyFile.SetStringList(group, key, value.([]string))
        }

        _, contents, err1 := keyFile.ToData()
        if err1 != nil {
                logger.Warning("KeyFile ToData Failed: %v\n", err1)
                return false
        }

        ok := writeStringToKeyFile(filename, string(contents))
        if !ok {
                return false
        }

        return true
}

func writeStringToKeyFile(filename, contents string) bool {
        if len(filename) <= 0 {
                return false
        }

        f, err := os.Create(filename + "~")
        if err != nil {
                logger.Warning("OpenFile '%s' failed: %v\n",
                        filename, err)
                return false
        }
        defer f.Close()

        if _, err = f.WriteString(contents); err != nil {
                logger.Warning("WriteString '%s' failed: %v\n",
                        filename, err)
                return false
        }
        f.Sync()
        os.Rename(filename+"~", filename)

        return true
}

func testKeyFIle() {
        writeKeyToKeyFile("/tmp/theme.ini", "Component", "GtkTheme", "Deepin-Blue")
        println("Has setted GtkTheme: Deepin-Blue")

        writeKeyToKeyFile("/tmp/theme.ini", "Component", "TestInt", int(12))

        strs := []string{"/var/1", "/var/2", "/var/4"}
        writeKeyToKeyFile("/tmp/theme.ini", "Component", "HistoryIcons", strs)

        strs = []string{"/var/1"}
        writeKeyToKeyFile("/tmp/theme.ini", "Component", "HistoryIcons", strs)
}

func main() {
        testKeyFIle()
}
